package tool.windows;

import java.awt.Point;
import java.io.UnsupportedEncodingException;
import java.util.List;

import com.google.common.collect.Lists;

import tool.dataType.MiddleCommonUtils;
import tool.dataType.WindowsClient;
import tool.windows.W32API.HWND;

public class WindowsTool {
	public static List<WindowsClient> mhClients(){
		List<WindowsClient> clients = Lists.newArrayList();
		W32API.HWND hwnd = null;
		while(true){
			byte[] text = new byte[1024];
			hwnd = User32.INSTANCE.FindWindowEx(null, hwnd, 0, 0);
			if(hwnd == null){
				break;
			}
			int length = User32.INSTANCE.GetWindowTextA(hwnd, text, 1024);
			try {
				String title = new String(text,0,length,"gb2312");
				if(title.indexOf("梦幻西游 ONLINE")!=-1){
					WindowsClient client = new WindowsClient();
					client.setHwnd(hwnd);
					client.setTitle(title);
					client.setShowName(MiddleCommonUtils.getName(title));
					client.setId(MiddleCommonUtils.getId(title));
					clients.add(client);
				}
			} catch (UnsupportedEncodingException e) {

				e.printStackTrace();
			}
		}
		return clients;
		
	}

	public static boolean getForce(HWND hwnd) {
		try {
				try {
					Thread.sleep(5);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				User32.INSTANCE.ShowWindow(hwnd, 9);
				try {
					Thread.sleep(5);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				User32.INSTANCE.SetForegroundWindow(hwnd);
				try {
					Thread.sleep(5);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public static void moveMouseRelative(Point p) {
		User32.INSTANCE.mouse_event(User32.WM_MOUSEMOVE, p.x, p.y, 0, null);
	}
}
